<?php

namespace app\admin\controller\bdl\pur;
use app\admin\model\BdOrg;
use app\admin\model\Material;
use app\admin\model\Materialimage;
use app\admin\model\Surface;
use app\common\controller\Backend;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Porder extends Backend
{
    /**
     * 模型对象
     * @var \app\admin\model\Porder
     */
    protected $model = null;


    protected $noNeedRight = ['look'];
    public function _initialize()
    {
        parent::_initialize();

        $this->model = new \app\admin\model\Porder();

        $this->view->assign("fenableList", $this->model->getFenableList());
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("typeList", $this->model->getTypeList());
        ini_set('memory_limit', '1024M');

    }



    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);

        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['modifier','creator','datacenter','forg','cust','address','carinfo'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            $qty        = [];
            $amount     = [];
            foreach ($list as $row)
            {
                $row['fk_bdl_allqty'] =floatval($row['fk_bdl_allqty']);
                $qty[$row['id']] = $row['fk_bdl_allqty'];
                $amount[$row['id']] = (string)$row['fk_bdl_allamount'];
            }

            $total = 0;
            foreach ($amount as $amounta) {
                $total += floatval($amounta);
            }

            $t['id'] = '合计';
            $t['fenable'] = '';
            $t['fk_bdl_comment'] = '';
            $t['fk_bdl_allqty'] = array_sum($qty);
            $t['fk_bdl_erpbillno'] = '';
            $t['fk_bdl_orderstatus'] = '';
            $t['fk_bdl_bd_useraddress'] = '';
            $t['fk_bdl_bd_useraddress_mes'] = '';
            $t['cust']['fname'] = '';
            $t['fk_bdl_allamount'] = number_format($total,2);
            $t['fk_bdl_ordertime'] = '';
            $t['fk_bdl_confirmtime'] = '';
            $t['fk_bdl_planshiptime'] = '';
            $t['fk_bdl_signtime'] = '';
            $t['fk_bdl_planreceivetime'] = '';
            $t['fk_bdl_returntime'] = '';
            $t['fk_bdl_signattach'] = '';
            $t['fk_bdl_materialbill'] = '';
            $t['fk_bdl_bizpersonlevel'] = '';
            $t['fk_bdl_carinfolevel'] = '';
            $t['fk_bdl_evaluateinfo'] = '';
            $t['datacenter.fname'] = '';
            $t['forg']['fname'] = '';
            $t['forg']['fnumber'] = '';
            $t['forg']['id'] = '';
            $t['fk_bdl_createtime'] = '';
            $t['fk_bdl_modifytime'] = '';

            $array = $list->items();
            $array[] = $t;
            $result = array("total" => $list->total(), "rows" => $array);

            return json($result);
        }
        return $this->view->fetch();
    }
    public function look($ids)
    {
        $datadata = \app\admin\model\Porder::with(['carinfo','address','cust'])->where('Porder.id',$ids)->find();
        //查找映射
        $datacenter = $datadata['fk_bdl_bd_datacenter'];
        $forgid     = $datadata['forgid'];
        $forg                   = BdOrg::get($forgid);
        $fnumbers               = explode(",",$forg['fk_bdl_mappingorg']);
        $forgids           = BdOrg::whereIn('fnumber',$fnumbers)->where('fk_bdl_bd_datacenter',$datacenter)->column('id');



        $new = [
            'fk_bdl_erpbillno'              => $datadata['fk_bdl_erpbillno'],
            'fk_bdl_allqty'                 => $datadata['fk_bdl_allqty'],
            'fk_bdl_allamount'              => $datadata['fk_bdl_allamount'],
            'fk_bdl_orderstatus'            => $datadata['fk_bdl_orderstatus'],
            'fk_bdl_ordertype'              => 1,


//            'fk_bdl_planshiptime'               => $datadata['fk_bdl_planshiptime'],
//            'fk_bdl_signtime'                   => $datadata['fk_bdl_signtime'],
//            'fk_bdl_planreceivetime'            => $datadata['fk_bdl_planreceivetime'],
            'id'                                => $datadata['id'],
            'carinfo'                           => $datadata['carinfo'],
            'address'                           => $datadata['address'],
            'cust'                           => $datadata['cust'],


            'fk_bdl_ordertime'                  => $datadata['fk_bdl_ordertime'],
            'fk_bdl_confirmtime'                => $datadata['fk_bdl_confirmtime'],
            'fk_bdl_planshiptime'               => $datadata['fk_bdl_planshiptime'],
            'fk_bdl_signtime'                   => $datadata['fk_bdl_signtime'],
            'fk_bdl_planreceivetime'            => $datadata['fk_bdl_planreceivetime'],
            'fk_bdl_signattach'                 => $datadata['fk_bdl_signattach'],
            'fk_bdl_materialbill'               => $datadata['fk_bdl_materialbill'],
            'fk_bdl_bizpersonlevel'             => $datadata['fk_bdl_bizpersonlevel'],
            'fk_bdl_carinfolevel'               => $datadata['fk_bdl_carinfolevel'],
            'fk_bdl_evaluateinfo'               => $datadata['fk_bdl_evaluateinfo'],
            'fk_bdl_orderstatus_text'           => $datadata['fk_bdl_orderstatus_text']

        ];

        $son = [];
        $dejson_data = json_decode($datadata['fk_bdl_orderjsondata'], true);



        if(!empty($dejson_data))
        {
            foreach ($dejson_data as $kk => $vv)
            {
                $img   = Material::where('fname',$vv['bdl_bd_material_name'])
                    ->whereIn('forgid',$forgids)
                    ->find();


                //拿到表面
                $face  = Surface::whereIn('forgid',$forgids)->where('fnumber',$vv['bdl_bd_surface_number'])
                    ->find();
                if(!empty($face))
                {
                    $surface_id     = $face['id'];
                    $material_id    = $img['id'];
                    if(!empty($surface_id) && !empty($material_id))
                    {
                        $find       = Materialimage::where('fk_bdl_bd_surface',$surface_id)->where('fk_bdl_bd_material',$material_id)->find();
                        if(!empty($find))
                        {
                            $show_image = $find['fk_bdl_imageurl'];
                        }else{
                            $show_image = '';
                        }
                    }else{
                        $show_image = '';
                    }

                }

                $son[] = [
                    'texture'           => !empty($vv['bdl_bd_texture_name']) ? $vv['bdl_bd_texture_name'] : '',
                    'surface'           => !empty($vv['bdl_bd_surface_name']) ? $vv['bdl_bd_surface_name'] : '',
                    'bdl_thickness'     => !empty($vv['bdl_thickness']) ? floatval($vv['bdl_thickness']) : '',
                    'bdl_width'         => !empty($vv['bdl_width']) ? floatval($vv['bdl_width']) : '',
                    'bdl_length'        => !empty($vv['bdl_length']) ? floatval($vv['bdl_length']) : '',
                    'bdl_refthickness'  => !empty($vv['bdl_refthickness']) ? floatval($vv['bdl_refthickness']) : '',
                    'bdl_unitid_name'   => !empty($vv['bdl_bd_material_bdl_unitid_name']) ? $vv['bdl_bd_material_bdl_unitid_name'] : '',
                    'bdl_amount'        => !empty($vv['bdl_salamt']) ? handle_money($vv['bdl_salamt']) : '',
                    'bdl_qty'           => !empty($vv['bdl_qty']) ? floatval($vv['bdl_qty']) : '',
                    'url'               => !empty($show_image) ? $show_image : ''
                ];
            }
        }

        $new['son'] = $son;
        return view('',[
            'data'  => $new
        ]);
    }

}